package han.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @fileName: OtherTools
 * @author: Han
 * @date: 2018/6/14 9:33
 * @dscription: TODO
 */
public class OtherTools {

    /**
     * 判断字符串是否是数字
     *
     * @param str 待检测字符串
     * @return 是否是数字
     */
    private static boolean isNumber(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void writeToLocal(String msg, String filePath, boolean append) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(filePath, append));
            writer.write(msg);
            writer.newLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(writer).close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 从本地读取文件内容
     *
     * @param filePath 文件路径
     * @return 文本内容
     */
    public static String readFromLocal(String filePath) {
        BufferedReader reader = null;
        String content = "";
        try {
            StringBuilder pageSB = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
            String line;
            while ((line = reader.readLine()) != null) {
                pageSB.append(line).append("\n");
            }
            content = pageSB.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(reader).close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return content;
    }

    /**
     * 加载本地公司id
     *
     * @return 公司id
     */
    public static List<String> getCompanyIdsByLocalFile(String fileName) {
        BufferedReader reader = null;
        List<String> companyIdSet = new ArrayList<>();
        try {
            reader = new BufferedReader(new FileReader(fileName));
            String line;
            while ((line = reader.readLine()) != null) {
                //过滤
                line = line.replace("\t", "");
                //如果不是数字,直接下一个
                if (!OtherTools.isNumber(line)) {
                    continue;
                }
                companyIdSet.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Objects.requireNonNull(reader).close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return companyIdSet;
    }

    /**
     * 将文件名中的汉字转为UTF8编码的串
     *
     * @param s 原串
     * @return 十六进制
     */
    public static String convertStringToUTF8(String s) {
        StringBuilder sb = new StringBuilder();
        try {
            char c;
            for (int i = 0; i < s.length(); i++) {
                c = s.charAt(i);
                if (c <= 255) {
                    sb.append(c);
                } else {
                    byte[] b;

                    b = Character.toString(c).getBytes("utf-8");

                    for (byte aB : b) {
                        int k = aB;
                        if (k < 0) {
                            k += 256;
                        }
                        sb.append("%").append(Integer.toHexString(k).toUpperCase());
                        // sb.append("%" +Integer.toHexString(k).toUpperCase());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return sb.toString();
    }
}
